package com.cardone.platform.usercenter.controller;

import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cardone.context.Contexts;
import com.cardone.common.dto.PaginationDto;
import com.cardone.common.dto.ReturnDataDto;
import com.cardone.common.util.ReturnDataUtils;
import com.cardone.context.ContextHolder;
import com.cardone.platform.usercenter.dto.DepartmentDto;
import com.cardone.platform.usercenter.service.DepartmentService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * 部门
 *
 * @author yaohaitao
 */
@Deprecated
@Setter
@Slf4j
@RequestMapping("/usercenter/department/")
@Controller
public class DepartmentController {

	/**
	 * 部门：删除
	 * 
	 * @param deleteDepartment
	 *            部门对象
	 * @return 处理结果，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "delete.json" }, method = RequestMethod.POST)
	@ResponseBody
  public Callable<ReturnDataDto<String>> deleteJson(final DepartmentDto deleteDepartment) {
		return new Callable<ReturnDataDto<String>>() {
			public ReturnDataDto<String> call() throws Exception {
				try {
					ContextHolder.getBean(DepartmentService.class).deleteByIds(deleteDepartment);
					
					return ReturnDataUtils.newReturnData();
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, log);
				}
			}
		};
	}

	/**
	 * 部门：编辑
	 * 
	 * @param findDepartment
	 *            部门对象
	 * @return 实体对象集，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "findById.json" })
	@ResponseBody
	public Callable<ReturnDataDto<DepartmentDto>> findByIdJson(final DepartmentDto findDepartment, final Model model) {
		return new Callable<ReturnDataDto<DepartmentDto>>() {
			@Override
			public ReturnDataDto<DepartmentDto> call() throws Exception {
				try {
					final DepartmentDto department = ContextHolder.getBean(DepartmentService.class).findById(DepartmentDto.class, findDepartment);

					return ReturnDataUtils.newReturnData(department);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, DepartmentController.log);
				}
			}
		};
	}

	/**
	 * 部门：分页查询
	 * 
	 * @param paginationDepartment
	 *            部门对象
	 * @return 分页，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "paginationByLikeCode.json" })
	@ResponseBody
  public Callable<ReturnDataDto<PaginationDto<DepartmentDto>>> paginationByLikeCodeJson(final DepartmentDto paginationDepartment) {
		return new Callable<ReturnDataDto<PaginationDto<DepartmentDto>>>() {
			public ReturnDataDto<PaginationDto<DepartmentDto>> call() throws Exception {
				try {
					final PaginationDto<DepartmentDto> departmentPagination = ContextHolder.getBean(DepartmentService.class).paginationByLikeCode(DepartmentDto.class, paginationDepartment);
				
					return ReturnDataUtils.newReturnData(departmentPagination);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, log);
				}
			}
		};
	}

	/**
	 * 部门：分页查询
	 * 
	 * @param paginationDepartment
	 *            部门对象
	 * @param model
	 *            上下文映射
	 */
	@Deprecated
	@RequestMapping(value = { "pagination.html" })
  public void paginationHtml(final DepartmentDto paginationDepartment, final Model model) {
		final PaginationDto<DepartmentDto> departmentPagination = ContextHolder.getBean(DepartmentService.class).paginationByLikeCode(DepartmentDto.class, paginationDepartment);
		
		model.addAttribute("departmentPagination", departmentPagination);
	}
	
	/**
	 * 部门：分页查询
	 * 
	 * @param findDepartment
	 *            部门对象
	 * @param model
	 *            上下文映射
	 */
	@Deprecated
	@RequestMapping(value = { "edit.html" })
  public void editHtml(final DepartmentDto findDepartment, final Model model) {
		final DepartmentDto department = ContextHolder.getBean(DepartmentService.class).findById(DepartmentDto.class, findDepartment);
		
		model.addAttribute("department", department);
	}

	/**
	 * 部门：查询
	 * 
	 * @param findListDepartment
	 *            部门对象
	 * @return 部门对象集合，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "findListByLikeCode.json" })
	@ResponseBody
  	public Callable<ReturnDataDto<List<DepartmentDto>>> findListByLikeCodeJson(final DepartmentDto findListDepartment) {
		return new Callable<ReturnDataDto<List<DepartmentDto>>>() {
			public ReturnDataDto<List<DepartmentDto>> call() throws Exception {
				try {
					final List<DepartmentDto> departmentList = ContextHolder.getBean(DepartmentService.class).findListByLikeCode(DepartmentDto.class, findListDepartment);
				
					return ReturnDataUtils.newReturnData(departmentList);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, log);
				}
			}
		};
	}

	/**
	 * 部门：保存
	 * 
	 * @param saveDepartment
	 *            部门对象
	 * @return 处理结果，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "save.json" })
	@ResponseBody
  public Callable<ReturnDataDto<String>> saveJson(final DepartmentDto saveDepartment) {
		return new Callable<ReturnDataDto<String>>() {
			public ReturnDataDto<String> call() throws Exception {
				try {
					ContextHolder.getBean(DepartmentService.class).saveByIdOrCode(DepartmentDto.class, saveDepartment);
					
					return ReturnDataUtils.newReturnData();
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, log);
				}
			}
		};
	}
}
